﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using TravelServer.DTOs;
using TravelServer.Models;
using TravelServer.ResourceParameters;
using TravelServer.Services;

namespace TravelServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly IMapper mapper;
        private readonly ITouristRoutsRepository routersRepository;
        private readonly IHttpContextAccessor contextAccessor;
        public OrderController(IHttpContextAccessor contextAccessor, ITouristRoutsRepository routersRepository, IMapper mapper)
        {
            this.contextAccessor = contextAccessor;
            this.routersRepository = routersRepository;
            this.mapper = mapper;
        }

        [HttpGet("orders")]
        public async Task<IActionResult> GetUserOrders([FromQuery]PaginationResourceParamaters paramater)
        {
            var userClainm = contextAccessor.HttpContext.User?.FindFirst(ClaimTypes.NameIdentifier);
            if (userClainm == null)
            {
                return Unauthorized("User ID not found");
            }
            var userId = userClainm.Value;
            var orders=await routersRepository.GetOrdersByUserIdAsync(userId,paramater.PageNumber,paramater.PageSize);
            return Ok(mapper.Map<ICollection<OrderDto>>(orders));
        }

        [HttpGet("orderId")]
        public async Task<IActionResult> GetOrderById([FromRoute] Guid orderId)
        {
            var order =await routersRepository.GetOrderByIdAsync(orderId);
            if (order == null)
            {
                return NotFound();
            }

            return Ok(mapper.Map<OrderDto>(order));
        }
    }
}
